package com.scsj.supermarket.view.fragment.classifymodel;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.CatgroyAdapter;
import com.scsj.supermarket.bean.CatgroyBean;
import com.scsj.supermarket.bean.CatgroyItemBean;
import com.scsj.supermarket.event.FirstEvent;
import com.scsj.supermarket.utils.GlideImageLoader;
import com.scsj.supermarket.utils.RxBus;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 分类Fragment
 */

public class ClassifyContentFragment extends BaseFragment {

    private List<CatgroyBean> data;
    private List<Integer> dataImg;
    private Banner banner;
    private RecyclerView recycler_view;
    private CatgroyAdapter mAdapter;
    private int type;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_classify_content, null);

        initView(view, null);
        initData();
        return view;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        banner = view.findViewById(R.id.banner);
        recycler_view = view.findViewById(R.id.recycler_view);
        recycler_view.setLayoutManager(new LinearLayoutManager(getHoldingActivity()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mAdapter = new CatgroyAdapter(getHoldingActivity());
        recycler_view.setAdapter(mAdapter);

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected int getLayoutId() {
        return 0;
    }


    public void initData() {

        RxBus.getDefault().toObservable(FirstEvent.class)
                //在io线程进行订阅，可以执行一些耗时操作
                .subscribeOn(Schedulers.io())
                //在主线程进行观察，可做UI更新操作
                .observeOn(AndroidSchedulers.mainThread())
                //观察的对象
                .subscribe(new Action1<FirstEvent>() {
                    @Override
                    public void call(FirstEvent user) {
                        if (user.getCode().equals("ClassifyContentFragment"))
                            exampleData(user.getData(),user.getTitle());
                    }
                });

        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        dataImg = new ArrayList<>();
        banner.setImages(dataImg);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        //内容对象创建完毕通知 ClassifytFragment
        RxBus.getDefault().post(new FirstEvent("ClassifyFragment",0));
    }

    private void exampleData(List list,String title) {
        mAdapter.clearData();
//        data = new ArrayList<>();
//        for (int i = 0; i < 1; i++) {
//            List<CatgroyItemBean> itemBeans = new ArrayList<>();
//            for (int j = 0; j < 18; j++) {
//                itemBeans.add(new CatgroyItemBean(R.mipmap.zzz_clothing, str + j));
//            }
//            data.add(new CatgroyBean(str, itemBeans));
//        }
        mAdapter.addAllData(list , title);
    }
}
